package leducHoldem.Data;

public class SimulationBoard extends Board {

	private Move currentCard;

	public SimulationBoard(Board board, Move currentCard) {
		super(board);
		this.currentCard = currentCard;
	}

	public int makeMoveForCard(Move move, Move card) {

		int value = super.makeMove(move);

		if (card != null) {
			addCardToId(card);
			currentCard = card;
		} else {
			addCardToId(currentCard);
		}

		return value;
	}

	public boolean isBeforeChanceMove(Move move) {

		if (makeMove(move) == 2) {
			reverseMove();
			return true;
		}

		reverseMove();

		return false;
	}
	
	public boolean isAfterChance() {
		return history.peek().equals(new Move("K")) || history.peek().equals(new Move("Q")) || history.peek().equals(new Move("J"));
	}

	public boolean isTerminalForMove(Move move) {

		makeMove(move);

		boolean isTerminal = isTerminal() != 0;

		reverseMove();

		return isTerminal;

	}

	private void addCardToId(Move card) {

		StringBuilder builder = new StringBuilder();

		builder.append(card);
		builder.append(id.substring(1));

		id = builder.toString();
	}

	public void reverseMove(Move card) {

		super.reverseMove();

		if (card != null) {
			currentCard = card;
			addCardToId(card);
		} else {
			addCardToId(currentCard);
		}
	}

	@Override
	public float[] evaluate(byte sign) {

		Dealer dealer = new Dealer();

		if (!history.isEmpty()) {
			if (history.peek().equals(new Move("f"))) {
				return sign == this.currentPlayer ? new float[] { pot, pot, pot } : new float[] { -pot, -pot, -pot };
			}
		}

		Move[] tempCards = new Move[2];
		float[] values = new float[3];
		int counter = 0;

		tempCards[currentPlayer - 1] = currentCard;

		for (Move card : dealer.getCardTypes()) {

			tempCards[2 - currentPlayer] = card;

			byte value = dealer.evaluate(tempCards[0], tempCards[1], table);

			if (value == (3 - sign)) {
				values[counter] = pot;
			} else if (value == sign) {
				values[counter] = -pot;
			}

			counter++;

		}

		return values;

	}

	public Move getCurrentCard() {
		return currentCard;
	}

}
